package com.tbynet.jwp.service.support;

import com.jfinal.kit.StrKit;
import com.tbynet.jwp.framework.exception.BusinessException;
import com.tbynet.jwp.service.dto.request.TermRequest;

import java.math.BigInteger;
import java.util.List;

/**
 * 分类项参数验证器
 * 负责所有分类项相关参数的验证逻辑
 */
public class TermValidator {

    /** 分类项名称最大长度 */
    public static final int MAX_NAME_LENGTH = 200;
    /** 分类项别名最大长度 */
    public static final int MAX_SLUG_LENGTH = 200;
    /** 搜索关键词最大长度 */
    public static final int MAX_SEARCH_LENGTH = 100;
    /** 描述最大长度 */
    public static final int MAX_DESCRIPTION_LENGTH = 500;

    private TermValidator() {
        // 工具类，防止实例化
    }

    /**
     * 验证请求参数
     * @param request
     */
    public static void validateRequest(TermRequest request) {
        if (request == null) {
            throw new BusinessException("request", "请求参数不能为空");
        }
    }

    /**
     * 验证分类项ID是否有效
     */
    public static void validateTermId(BigInteger termId) {
        if (termId == null || termId.compareTo(BigInteger.ZERO) <= 0) {
            throw new BusinessException("termId", "无效的分类项ID: " + termId);
        }
    }

    /**
     * 验证分类法类型是否有效
     */
    public static void validateTaxonomy(String taxonomy) {
        if (StrKit.isBlank(taxonomy)) {
            throw new BusinessException("taxonomy", "分类法类型不能为空");
        }
    }

    /**
     * 验证分类项名称是否有效
     */
    public static void validateTermName(String name) {
        if (StrKit.isBlank(name)) {
            throw new BusinessException("termName", "分类项名称不能为空");
        }
        if (name.length() > MAX_NAME_LENGTH) {
            throw new BusinessException("termName", "分类项名称长度不能超过" + MAX_NAME_LENGTH + "个字符");
        }
    }

    /**
     * 验证分类项别名是否有效
     */
    public static void validateSlug(String slug) {
        if (StrKit.isBlank(slug)) {
            throw new BusinessException("slug", "分类项别名不能为空");
        }
        if (slug.length() > MAX_SLUG_LENGTH) {
            throw new BusinessException("slug", "分类项别名长度不能超过" + MAX_SLUG_LENGTH + "个字符");
        }
        if (!slug.matches("^[a-z0-9-_]+$")) {
            throw new BusinessException("slug", "分类项别名只能包含小写字母、数字、连字符和下划线");
        }
    }

    /**
     * 验证搜索关键词
     */
    public static void validateSearchKeyword(String keyword) {
        if (StrKit.isBlank(keyword)) {
            throw new BusinessException("keyword", "搜索关键词不能为空");
        }
        if (keyword.length() > MAX_SEARCH_LENGTH) {
            throw new BusinessException("keyword", "搜索关键词长度不能超过" + MAX_SEARCH_LENGTH + "个字符");
        }
    }

    /**
     * 验证描述信息
     */
    public static void validateDescription(String description) {
        if (description != null && description.length() > MAX_DESCRIPTION_LENGTH) {
            throw new BusinessException("description", "描述信息长度不能超过" + MAX_DESCRIPTION_LENGTH + "个字符");
        }
    }

    /**
     * 验证创建分类项参数
     */
    public static void validateCreateParams(String name, String taxonomy) {
        validateTermName(name);
        validateTaxonomy(taxonomy);
    }

    /**
     * 验证更新分类项参数
     */
    public static void validateUpdateParams(BigInteger termId, String taxonomy) {
        validateTermId(termId);
        validateTaxonomy(taxonomy);
    }

    /**
     * 验证批量操作参数
     */
    public static void validateBatchParams(List<BigInteger> termIds, String taxonomy) {
        if (termIds == null || termIds.isEmpty()) {
            throw new BusinessException("termIds", "分类项ID列表不能为空");
        }
        if (termIds.size() > 100) {
            throw new BusinessException("termIds", "批量操作不能超过100个分类项");
        }
        validateTaxonomy(taxonomy);

        for (BigInteger termId : termIds) {
            validateTermId(termId);
        }
    }

    /**
     * 验证分页参数
     */
    public static void validatePageParams(Integer pageNumber, Integer pageSize) {
        if (pageNumber == null || pageNumber <= 0) {
            throw new BusinessException("pageNumber", "页码必须大于0");
        }
        if (pageSize == null || pageSize <= 0 || pageSize > 1000) {
            throw new BusinessException("pageSize", "每页大小必须在1-1000之间");
        }
    }

    /**
     * 验证父级分类项ID
     */
    public static void validateParentId(BigInteger parentId, String taxonomy) {
        if (parentId != null && parentId.compareTo(BigInteger.ZERO) < 0) {
            throw new BusinessException("parentId", "父级分类项ID无效: " + parentId);
        }
        // 对于非层级分类法，不允许设置父级
        if (parentId != null && !"category".equals(taxonomy) && !"nav_menu".equals(taxonomy)) {
            throw new BusinessException("taxonomy", "分类法 " + taxonomy + " 不支持层级结构");
        }
    }
}